package base;
/*
 *  大整数加法
 * 
 * */
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BigNumberAdd {

	public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
		System.out.println("输入加数1：");
		String str1 = sc.nextLine();
		System.out.println("输入加数2：");
		String str2 = sc.nextLine();
		String s =add(str1,str2);
		System.out.println("结果为："+s);
	}

	public static String add(String str1, String str2) {
		//判断两个数是否为空且是否满足正则规则
		if(isNull(str1,str2)){
		  return null;
		}
		if (str1.equals("0")) {
			return str2;
		}
		if (str2.equals("0")) {
			return str1;
		}
		if (str1.length()>str2.length()) { // 交换加数位置
			String tmp = str1;
			str1 = str2;
			str2 = tmp;
		}
		  str1 =addZone(str1,str2.length());
		  String s =addMathod(str1,str2);
		return s;
	}
	
	//计算两个大整数相加
	private static String addMathod(String s1,String s2){  
        String str="";
        int len=s1.length();
        int[] a=toArray(s1);
        int[] b=toArray(s2);
        int[] c=addArray(a, b);
       
        //转换成字符串
        StringBuilder sb=new StringBuilder(32);
        for(int i=0;i<=len;i++){
            sb.append(c[i]);
        }
        if(c[0]==0){ //删除字符串中的第一个0;
            str=sb.substring(1);
        }else{
            str=sb.toString();
        }
        return str;
    }
   
	//数组相加
	private static int[] addArray(int[] a, int[] b) {
		if (a == null)
			return b;
		if (b == null)
			return a;
		if (a.length != b.length) {
			// 判断两个数组的长度
			int[] temp = null;
			if (a.length < b.length) {
				temp = b;
				b = a;
				a = temp;
			}
			temp = new int[a.length];

			// 补齐 b的长度
			for (int i = a.length - 1, j = b.length - 1; i > 0; i--) {
				if (j > 0) {
					temp[i] = b[j--];
				} else {
					temp[i] = 0;
				}
			}
			b = temp;
		}

		int len = a.length;
		int c[] = new int[len + 1];// 存放结果

		int point = 0; // 进位标志

		for (int i = 0;i<len; i++) {
			int sum = a[len - 1 - i] + b[len - 1 - i] + point;
		if(sum>=10){
				c[i]= sum%10;
			}
			else
			c[i] = sum;
			point = sum / 10; // 进位数
		}
		c[len] = point;
     
		for(int i=0,j=c.length-1;i<j;i++,j--){
			int temp =c[i];
			c[i]=c[j];
			c[j]=temp;
		}
		
		return c;
	}
	
	//字符串装换成数组
	private static int[] toArray(String str){
		int lenth =str.length();
		int[] arr =new int[lenth];
		for(int i=0;i<lenth;i++){
			arr[i]=str.charAt(i)-'0'; //将字符 一个个转换成数字存入数组中
		}
		
		return arr;
	}

    //在取出的字符前面添加 length 个零
	private static String addZone(String str,int length){
		StringBuilder sb = new StringBuilder();
		int diffent =length -str.length();
		while(diffent>0){
		sb.append("0");
		diffent--;
		}
		sb.append(str);
		return sb.toString();
	}

	private static boolean isNull(String str1, String str2) {
		if (str1.isEmpty() && str2.isEmpty() && !isMatcher(str1) && !isMatcher(str2))
			return true;
		return false;
	}
	
	  //判断是否满足正则表达式
	private static boolean isMatcher(String str){
		 Pattern p=Pattern.compile("[0-9]*");
	        Matcher isNum=p.matcher(str);
	        return isNum.matches();
	}
	
}
